import {Controller} from "egg";
import {Result} from "../util/result";
import MsgEnum from "../util/msgEnum";
import {v4 as uuidv4} from "uuid";
import {USER_STATE, USER_IDENTITY} from "../../constants/user-role";

export default class UserController extends Controller {
  public async deleteUsers() {
    const {ctx} = this;
    const {id} = ctx.request.body;
    const result = await this.ctx.service.user.deleteUsers(id);
    if (result.mainData) {
      ctx.body = result;
    } else {
      ctx.status = 406;
      ctx.body = result;
    }
  }

  public async uploadUser() {
    const {ctx} = this;
    const {
      id,
      education,
      name,
      post,
      address,
      sex,
      major,
      password,
      tel,
      email,
      age,
      identity,
      state,
      department,
      role,
      post_uuid,
      role_uuid,
    } = ctx.request.body;

    const result = await this.ctx.service.user.uploadUser(
      id,
      education,
      name,
      post,
      address,
      sex,
      major,
      department,
      password,
      tel,
      email,
      age,
      identity,
      state,
      role,
      post_uuid,
      role_uuid
    );
    if (result.mainData) {
      ctx.body = result;
    } else {
      ctx.status = 406;
      ctx.body = result;
    }
  }

  public async freezeUser() {
    const {ctx} = this;
    const {id, cold} = ctx.request.body;
    const result = await this.ctx.service.user.freezeUser(id, cold);
    if (result.mainData) {
      ctx.body = result;
    } else {
      ctx.status = 406;
      ctx.body = result;
    }
  }

  public async deleteUser() {
    const {ctx} = this;
    const {id, state} = ctx.request.body;
    const result = await this.ctx.service.user.deleteUser(id, state);
    if (result.mainData) {
      ctx.body = result;
    } else {
      ctx.status = 406;
      ctx.body = result;
    }
  }

  public async createUser() {
    const {ctx} = this;
    const {
      education,
      name,
      post,
      address,
      sex,
      major,
      department,
      password,
      tel,
      email,
      age,
      identity,
      state,
      role,
    } = ctx.request.body;

    const result = await this.ctx.service.user.crateUser(
      education,
      name,
      post,
      address,
      sex,
      major,
      department,
      password,
      tel,
      email,
      age,
      identity,
      state,
      role
    );
    if (result.mainData) {
      ctx.body = result;
    } else {
      ctx.status = 406;
      ctx.body = result;
    }
  }

  public async getUserList() {
    const {ctx, app} = this;

    const errors = app.validator.validate(
      {
        page: {require, type: "int", convertType: "int"},
      },
      ctx.query
    );
    if (errors) {
      ctx.status = 401;
      ctx.body = new Result(errors, MsgEnum.PARAMETER_VERIFICATION_ERROR);
    } else {
      const {page, phone, email, name, role, states} = ctx.query;
      const result = await this.ctx.service.user.getUserList(
        Number(page),
        phone,
        email,
        name,
        role,
        states
      );
      if (result.mainData) {
        ctx.body = result;
      } else {
        ctx.status = 406;
        ctx.body = result;
      }
    }
  }

  async getUserInfo() {
    const {ctx, app} = this;

    const errors = app.validator.validate(
      {
        id: {require, type: "string"},
      },
      ctx.request.body
    );
    if (errors) {
      ctx.status = 400;
      ctx.body = new Result(errors, MsgEnum.PARAMETER_VERIFICATION_ERROR);
    } else {
      const {id} = ctx.request.body;
      const result = await this.ctx.service.user.findUserById(id);
      if (result.mainData) {
        ctx.body = result;
      } else {
        ctx.status = 406;
        ctx.body = result;
      }
    }
  }

  async changeUserInfo() {
    const {ctx, app} = this;

    const errors = app.validator.validate(
      {
        id: {require, type: "string"},
        education: {require, type: "string"},
        name: {require, type: "string"},
        post: {require, type: "string"},
        address: {require, type: "string"},
        sex: {require, type: "int"},
        major: {require, type: "string"},

        department: {require, type: "string"},
      },
      ctx.request.body
    );
    errors;
    if (errors) {
      ctx.status = 400;
      ctx.body = new Result(errors, MsgEnum.PARAMETER_VERIFICATION_ERROR);
    } else {
      const {
        id,
        education,
        name,
        post,
        address,
        sex,
        major,

        department,
      } = ctx.request.body;

      const result = await this.ctx.service.user.changUserInfo(
        id,
        education,
        name,
        post,
        address,
        sex,
        major,

        department
      );
      if (result.mainData) {
        ctx.body = result;
      } else {
        ctx.status = 406;
        ctx.body = result;
      }
    }
  }

  async hasSameTel() {
    const {ctx} = this;
    const {tel} = ctx.request.body;
    if (tel) {
      const user = await ctx.model.User.findAll({
        where: {tel},
      });
      const res = user.length !== 0;
      ctx.body = new Result(res);
    }
    /* 当tel参数为空时默认返回一个值,代表不进行检测 */
    return new Result(true);
  }

  async registerUser() {
    const {ctx} = this;
    const {
      tel,
      password,
      username,
      address,
      organization,
      title,
      position,
      email,
    } = ctx.request.body;
    let transaction;
    try {
      transaction = await this.ctx.model.transaction();
      await this.ctx.model.User.create(
        {
          // age: ,
          department: organization,
          name: username,
          fk_organization: "",
          state: USER_STATE.UNCHECK,
          identity: USER_IDENTITY.MANAGER,
          role: title,
          post: position,
          uuid: uuidv4(),
          tel,
          email,
          // sex: '',
          address: address.join(";"),
          password,
          // education: '',
          // major: '',
        },
        {transaction}
      );
      await transaction.commit();
      ctx.body = new Result({code: 0}, MsgEnum.UPLOAD_SUCCESS);
    } catch (e) {
      console.log(e);
      await transaction.rollback();
      ctx.body = new Result({code: 100}, MsgEnum.SERVER_ERROR);
    }
  }
}
